package com.movecar.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.movecar.R;
import com.movecar.adapter.ImageAdapter;
import com.movecar.dialog.CustomDialog;
import com.movecar.listview.MyListView;
import com.movecar.model.CloudBeansBean;
import com.movecar.model.GroupBean;
import com.movecar.model.HomeInfoBean;
import com.movecar.model.Pictures;
import com.movecar.model.ShareCommentBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.Converter;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.PopupWindowAnimation;
import com.movecar.utils.RequestResultCallback;
import com.movecar.utils.imageloader.Constants;
import com.movecar.widget.CustomProgressDialog;
import com.movecar.widget.MyGridView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rockerhieu.emojicon.EmojiconEditText;
import com.rockerhieu.emojicon.EmojiconGridFragment;
import com.rockerhieu.emojicon.EmojiconTextView;
import com.rockerhieu.emojicon.EmojiconsFragment;
import com.rockerhieu.emojicon.emoji.Emojicon;

/**
 * 主页-分享列表-评论
 * 
 * @author Zhao.JQ
 * 
 */

public class AddShareDiscussActivity extends FragmentActivity implements android.view.View.OnClickListener,
		EmojiconGridFragment.OnEmojiconClickedListener, EmojiconsFragment.OnEmojiconBackspaceClickedListener {

	private CustomProgressDialog progressDialog = null;
	protected MyListView mList;
	private CloudBeansBean mCloudBeansBean;
	private HomeInfoBean mHomeInfoBean;
	private ImageLoader imageLoader = null;
	private InputMethodManager imm;
	private List<ShareCommentBean> mDataList = new ArrayList<ShareCommentBean>();

	private String shareId, messageId, zanCount, is_zan, ooflag, address, rbMsgId, ssType, sectionId;
	private int position;
	private Button back, ok, emojicons_button, delete;
	private EmojiconEditText edit_discuss;
	private String commentId;
	private String replyUserId;
	private DataAdapter adapter;
	private String userIcon, time, content, main_img, name, main_img_big;

	private List<HomeInfoBean> resultThumbList = null;
	private List<HomeInfoBean> resultOriginalList = null;
	private RelativeLayout mRelativeLayout;
	private boolean isreplyed = false;
	private boolean isPraise = false;
	private android.view.animation.Animation animation;
	private ImageView user_img;
	private ImageView main_big;
	private TextView user_name;
	private TextView creat_time;
	private TextView location;
	private EmojiconTextView describe_info;
	private TextView joined_button;
	private Button zan;
	private Button zan_click;
	private TextView zan_count;
	private MyGridView mGridView;
	private LinearLayout emojicons_linear;
	private RelativeLayout add_discuss;
	private RelativeLayout describe_info_relative;
	private View mainView;
	private LayoutInflater layoutInflater;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);mainView = layoutInflater.inflate(R.layout.add_share_discuss, null);
		setContentView(mainView);
		shareId = getIntent().getStringExtra("shareId");
		imageLoader = ImageLoader.getInstance();
		startProgressDialog();
		getDiscussList();

		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		animation = AnimationUtils.loadAnimation(this, R.anim.zan_anim);
		initView();
	}

	private void initView() {
		edit_discuss = (EmojiconEditText) findViewById(R.id.edit_discuss);
		edit_discuss.setEmojiconSize(Converter.dp2pixel(AddShareDiscussActivity.this, 20));
		edit_discuss.setOnClickListener(this);

		back = (Button) findViewById(R.id.btn_left);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		delete = (Button) findViewById(R.id.btn_right);
		delete.setBackgroundResource(R.drawable.btn_share_delete);
		delete.setOnClickListener(this);
		emojicons_button = (Button) findViewById(R.id.emojicons_button);
		emojicons_button.setOnClickListener(this);
		mRelativeLayout = (RelativeLayout) findViewById(R.id.friends_message);
		user_img = (ImageView) findViewById(R.id.user_img);
		main_big = (ImageView) findViewById(R.id.main_img);
		user_name = (TextView) findViewById(R.id.user_name);
		creat_time = (TextView) findViewById(R.id.time);
		location = (TextView) findViewById(R.id.location);
		describe_info = (EmojiconTextView) findViewById(R.id.describe_info);
		describe_info.setEmojiconSize(Converter.dp2pixel(AddShareDiscussActivity.this, 20));
		joined_button = (TextView) findViewById(R.id.joined_button);
		zan = (Button) findViewById(R.id.zan);
		zan_click = (Button) findViewById(R.id.zan_click);
		zan_count = (TextView) findViewById(R.id.zan_text);
		joined_button.setOnClickListener(this);
		emojicons_linear = (LinearLayout) findViewById(R.id.emojicons_linear);
		add_discuss = (RelativeLayout) findViewById(R.id.add_discuss);
		mGridView = (MyGridView) findViewById(R.id.mGridView);
		describe_info_relative = (RelativeLayout) findViewById(R.id.describe_info_relative);
		Intent intent = getIntent();
		position = intent.getIntExtra("position", 0);
		startProgressDialog();
		getShareInfo();
	}

	@SuppressWarnings("unchecked")
	private void setInfo() {
		ArrayList<Pictures> picList = new ArrayList<Pictures>();
		ImageAdapter adapter = new ImageAdapter(AddShareDiscussActivity.this, picList);
		mGridView.setAdapter(adapter);
		// if ("0".equals(messageId)) {
		// message_arrow.setVisibility(View.GONE);
		// joined_button.setVisibility(View.GONE);
		// }
		if (!TextUtils.isEmpty(address)) {
			location.setText(address);
		} else {
			location.setVisibility(View.GONE);
		}

		if ("0".equals(is_zan)) {
			zan.setBackgroundResource(R.drawable.zan);
		} else {
			zan.setBackgroundResource(R.drawable.zan_click);

		}
		zan_count.setText(zanCount);
		// 设置头像
		if (userIcon.contains("default")) {
			imageLoader.displayImage(AppConfig.HOST + userIcon, user_img, Constants.image_display_options,
					AppConfig.animateListener);
		} else {
			imageLoader.displayImage(AppConfig.HOST + userIcon, user_img, Constants.image_display_options_conner,
					AppConfig.animateListener);
		}
		user_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if ("0".equals(mHomeInfoBean.getIsSectionShare())) {
					if (!mHomeInfoBean.getShareUserId().equals(DataStore.getInstance().getUserId())) {
						Intent intent = new Intent().setClass(AddShareDiscussActivity.this,
								FriendPersonalInfActivity.class);
						intent.putExtra("shareUserId", mHomeInfoBean.getShareUserId());
						startActivity(intent);
					} else {

						Intent intent = new Intent().setClass(AddShareDiscussActivity.this, PersonalInfActivity.class);
						startActivity(intent);

					}
				} else if ("1".equals(mHomeInfoBean.getIsSectionShare())) {
					Intent intent = new Intent().setClass(AddShareDiscussActivity.this, ClubDetailActivity.class);
					intent.putExtra("sectionId", mHomeInfoBean.getShareUserId());
					startActivity(intent);
				}

			}
		});
		if ("0".equals(mHomeInfoBean.getOoFlag())) {
			joined_button.setVisibility(View.GONE);
		} else if ("1".equals(ooflag)) {
			joined_button.setVisibility(View.VISIBLE);
			joined_button.setBackgroundResource(R.drawable.get_chance);
		} else if ("2".equals(ooflag)) {
			joined_button.setVisibility(View.VISIBLE);
			joined_button.setBackgroundResource(R.drawable.get_good);
		} else if ("3".equals(ooflag)) {
			joined_button.setVisibility(View.VISIBLE);
			joined_button.setBackgroundResource(R.drawable.home_go_club);

		} else if ("4".equals(ooflag)) {
			joined_button.setVisibility(View.VISIBLE);
			joined_button.setBackgroundResource(R.drawable.have_red_packet);

		} else if ("5".equals(ooflag)) {
			joined_button.setVisibility(View.VISIBLE);
			joined_button.setBackgroundResource(R.drawable.no_red_packet);

		} else {

		}
		user_name.setText(name);
		describe_info.setText(content);
		creat_time.setText(time);

		Type type2 = new TypeToken<List<HomeInfoBean>>() {
		}.getType();
		Gson gson2 = new Gson();
		Object fromJson2 = gson2.fromJson(this.main_img, type2);
		Object fromJson3 = gson2.fromJson(this.main_img_big, type2);
		try {
			resultThumbList = (List<HomeInfoBean>) fromJson2;
			resultOriginalList = (List<HomeInfoBean>) fromJson3;
		} catch (Exception e) {
			return;
		}
		zan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				int zan_num = Integer.parseInt(zanCount);

				if ("0".equals(is_zan)) {
					praiseShare(shareId, position);
					zan_click.setVisibility(View.VISIBLE);
					zan_click.startAnimation(animation);
					new Handler().postDelayed(new Runnable() {
						public void run() {
							zan_click.setVisibility(View.GONE);
						}
					}, 1000);
					zan.setBackgroundResource(R.drawable.zan_click);

					is_zan = ("1");

					if (zan_num >= 0) {
						zan_num = zan_num + 1;
					}
					zan_count.setText(String.valueOf(zan_num));
					zanCount = (String.valueOf(zan_num));
					isPraise = true;
				} else {
				}

			}
		});
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				if (resultThumbList.get(arg2) != null && resultThumbList.size() > 0) {

					Intent intent = new Intent(AddShareDiscussActivity.this, ClubPhotoActivity.class);
					ArrayList<Pictures> list = new ArrayList<Pictures>();
					if (null != resultOriginalList) {
						list.clear();
						for (int i = 0; i < resultOriginalList.size(); i++) {

							Pictures p = new Pictures();
							p.setUrl(resultOriginalList.get(i).getOriginalUrl());
							list.add(p);
						}
					}

					intent.putExtra("Pictures", list);
					intent.putExtra("currentId", arg2);
					startActivity(intent);
				}
			}
		});
		if (null == resultThumbList) {
			mGridView.setVisibility(View.GONE);
			main_big.setVisibility(View.GONE);
		} else {
			if (null != resultThumbList && resultThumbList.size() == 0) {
				mGridView.setVisibility(View.GONE);
				main_big.setVisibility(View.GONE);
			} else if (resultThumbList.size() == 1) {
				main_big.setVisibility(View.VISIBLE);
				imageLoader.displayImage(AppConfig.HOST + resultThumbList.get(0).getThumbUrl(), main_big,
						Constants.image_display_options, AppConfig.animateListener);
				main_big.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						Intent intent = new Intent(AddShareDiscussActivity.this, ClubPhotoActivity.class);
						ArrayList<Pictures> list = new ArrayList<Pictures>();
						if (null != resultOriginalList) {
							list.clear();

							Pictures p = new Pictures();
							p.setUrl(resultOriginalList.get(0).getOriginalUrl());
							list.add(p);

						}

						intent.putExtra("Pictures", list);
						startActivity(intent);

					}
				});

			} else if (resultThumbList.size() == 2 || resultThumbList.size() == 4) {
				main_big.setVisibility(View.GONE);
				picList.clear();
				for (int n = 0; n < resultThumbList.size(); n++) {
					if (n < 6) {
						Pictures p = new Pictures();
						p.setUrl(resultThumbList.get(n).getThumbUrl());
						picList.add(p);
					}
				}
				mGridView.setNumColumns(2);
				mGridView.setVisibility(View.VISIBLE);
				adapter.notifyDataSetChanged();
			} else {
				main_big.setVisibility(View.GONE);
				if (resultThumbList.size() > 6) {
					picList.clear();
					for (int n = 0; n < resultThumbList.size(); n++) {
						if (n < 6) {
							Pictures p = new Pictures();
							p.setUrl(resultThumbList.get(n).getThumbUrl());
							picList.add(p);
						}
					}
				} else if (resultThumbList.size() > 1) {
					picList.clear();
					for (int n = 0; n < resultThumbList.size(); n++) {
						Pictures p = new Pictures();
						p.setUrl(resultThumbList.get(n).getThumbUrl());
						picList.add(p);
					}

				}
				mGridView.setNumColumns(3);
				mGridView.setVisibility(View.VISIBLE);
				adapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 点赞
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void praiseShare(String shareId, final int position) {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "praiseShareNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("shareId", shareId));
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if ("0".equals(mCloudBeansBean.getResultCode())) {
					if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
						new PopupWindowAnimation(mainView, AddShareDiscussActivity.this);
					}
					stopProgressDialog();

				} else {
					stopProgressDialog();
					Alert.error(AddShareDiscussActivity.this, result);
				}
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(AddShareDiscussActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.btn_left:
			if (isreplyed && !isPraise) {
				Intent i = new Intent();
				i.putExtra("position", position);
				i.putExtra("isreplyed", true);
				setResult(RESULT_OK, i);
				isreplyed = false;
			} else if (isPraise && !isreplyed) {
				Intent i = new Intent();
				i.putExtra("position", position);
				i.putExtra("zanCount", zanCount);
				setResult(RESULT_OK, i);
				isPraise = false;
			} else if (isreplyed && isPraise) {
				Intent i = new Intent();
				i.putExtra("position", position);
				i.putExtra("zanCount", zanCount);
				i.putExtra("isreplyed", true);
				setResult(RESULT_OK, i);
				isPraise = false;
				isreplyed = false;
			} else {
				Intent i = new Intent();
				i.putExtra("position", position);
				setResult(RESULT_OK, i);

			}
			finish();
			break;
		case R.id.ok:
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			if (emojicons_linear.getVisibility() == View.VISIBLE) {
				emojicons_linear.setVisibility(View.GONE);
				emojicons_button.setBackgroundResource(R.drawable.discuss_emoji);
				RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) add_discuss.getLayoutParams();
				params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				add_discuss.setLayoutParams(params);
			}
			if ("".equals(edit_discuss.getText().toString().trim())) {
				Alert.error(AddShareDiscussActivity.this, "回复内容不能为空");
			} else if (edit_discuss.getText().toString().indexOf("@") == 0) {
				isreplyed = true;
				startProgressDialog();
				replyShareComment(commentId, replyUserId);
			} else {
				isreplyed = true;
				startProgressDialog();
				replyShareComment(commentId, "0");
			}
			break;
		case R.id.joined_button:

			if ("1".equals(ooflag)) {
				intent.setClass(AddShareDiscussActivity.this, GroupDetailActivity.class);
				intent.putExtra("from_home", true);
				intent.putExtra("messageId", messageId);
				startActivity(intent);
			} else if ("2".equals(ooflag)) {
				startProgressDialog();
				joinToo(shareId, rbMsgId);
				joined_button.setVisibility(View.GONE);
			} else if ("3".equals(ooflag)) {
				if ("0".equals(ssType)) {

					intent.setClass(AddShareDiscussActivity.this, ClubDetailActivity.class);
					intent.putExtra("sectionId", sectionId);
					startActivity(intent);
				} else if ("2".equals(ssType)) {

					intent.setClass(AddShareDiscussActivity.this, RecommendClubDetailActivity.class);
					intent.putExtra("sectionId", sectionId);
					startActivity(intent);
				} else {

				}
			} else {

			}

			joinToo(shareId, messageId);

			break;
		case R.id.emojicons_button:
			if (emojicons_linear.getVisibility() == View.VISIBLE) {
				emojicons_button.setBackgroundResource(R.drawable.discuss_emoji);
				emojicons_linear.setVisibility(View.GONE);
				RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) add_discuss.getLayoutParams();
				params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				add_discuss.setLayoutParams(params);
			} else {
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
				RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
						LayoutParams.WRAP_CONTENT);
				add_discuss.setLayoutParams(p);
				RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) add_discuss.getLayoutParams();
				params2.addRule(RelativeLayout.ABOVE, R.id.emojicons_linear);
				add_discuss.setLayoutParams(params2);
				emojicons_linear.setVisibility(View.VISIBLE);
				emojicons_button.setBackgroundResource(R.drawable.discuss_keyboard);
			}

			break;
		case R.id.edit_discuss:
			emojicons_button.setBackgroundResource(R.drawable.discuss_emoji);
			emojicons_linear.setVisibility(View.GONE);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) add_discuss.getLayoutParams();
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			add_discuss.setLayoutParams(params);

			break;

		case R.id.btn_right:

			CustomDialog.Builder customBuilder = new CustomDialog.Builder(AddShareDiscussActivity.this);
			ImageView img = new ImageView(AddShareDiscussActivity.this);
			img.setImageResource(R.drawable.ic_launcher);
			customBuilder.setImgIcon(img).setMessage("确认删除此次分享?")

			.setPositiveButton("确认", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startProgressDialog();
					deleteSahre();
					dialog.dismiss();

				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {

					dialog.dismiss();

				}
			});
			customBuilder.create().show();

			break;
		default:
			break;
		}

	}

	class DataAdapter extends BaseAdapter {

		private Activity activity;
		private LayoutInflater inflater;

		DataAdapter(Activity activity) {

			this.activity = activity;
			inflater = LayoutInflater.from(activity);
		}

		@Override
		public int getCount() {

			return mDataList.size();
		}

		@Override
		public Object getItem(int arg0) {

			return mDataList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {

			return arg0;
		}

		@Override
		public View getView(final int position, View view, ViewGroup arg2) {
			View itemView = null;

			if (view == null) {
				// viewHolder = new ViewHolder();
				view = inflater.inflate(R.layout.add_share_discuss_item, null);
				itemView = view;
				view.setTag(itemView);

				// view.setTag(viewHolder);
			} else {
				itemView = (View) view.getTag();
				// viewHolder = (ViewHolder) view.getTag();
			}
			ImageView user_img = (ImageView) itemView.findViewById(R.id.user_img);
			TextView user_name = (TextView) itemView.findViewById(R.id.user_name);
			EmojiconTextView discuss_info = (EmojiconTextView) itemView.findViewById(R.id.discuss_info);
			discuss_info.setEmojiconSize(Converter.dp2pixel(AddShareDiscussActivity.this, 20));
			TextView add_time = (TextView) itemView.findViewById(R.id.add_time);
			Button reply = (Button) itemView.findViewById(R.id.reply);
			if (mDataList.get(position).getScUserIcon().contains("default")) {
				imageLoader.displayImage(AppConfig.HOST + mDataList.get(position).getScUserIcon(), user_img,
						Constants.image_display_options);
			} else {
				imageLoader.displayImage(AppConfig.HOST + mDataList.get(position).getScUserIcon(), user_img,
						Constants.image_display_options_conner);
			}
			user_img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (!mDataList.get(position).getScUserId().equals(DataStore.getInstance().getUserId())) {
						Intent intent = new Intent().setClass(AddShareDiscussActivity.this,
								FriendPersonalInfActivity.class);
						intent.putExtra("shareUserId", mDataList.get(position).getScUserId());
						startActivity(intent);
					} else {

						Intent intent = new Intent().setClass(AddShareDiscussActivity.this, PersonalInfActivity.class);
						startActivity(intent);

					}

				}
			});
			if (DataStore.getInstance().getUserId().equals(mDataList.get(position).getScUserId())) {
				reply.setVisibility(View.INVISIBLE);
			} else {
				reply.setVisibility(View.VISIBLE);
			}
			discuss_info.setText(mDataList.get(position).getScContent());
			add_time.setText(mDataList.get(position).getScCreateTime());
			if (!"".equals(mDataList.get(position).getReplyUserNickname())) {
				user_name.setText(mDataList.get(position).getScUserNickname());
			} else {
				user_name.setText(mDataList.get(position).getScUserNickname());
			}

			reply.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					edit_discuss.setText("@" + mDataList.get(position).getScUserNickname() + ":");
					imm.showSoftInput(edit_discuss, InputMethodManager.RESULT_SHOWN);
					imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
					edit_discuss.requestFocus();
					edit_discuss.setSelection(mDataList.get(position).getScUserNickname().length() + 2);

					commentId = mDataList.get(position).getCommentId();
					replyUserId = mDataList.get(position).getScUserId();
				}
			});
			return view;
		}
	}

	@SuppressWarnings("unchecked")
	private void getDiscussList() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryShareCommentNew1"));
		// 添加要传递的参数
		params.add(new BasicNameValuePair("shareId", shareId));
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				Type type = new TypeToken<List<ShareCommentBean>>() {
				}.getType();
				Gson gson = new Gson();
				String result = (String) object;
				Object fromJson = gson.fromJson(result, type);
				try {
					mDataList = (List<ShareCommentBean>) fromJson;
				} catch (Exception e) {
					return;
				}
				if (mDataList != null) {
					stopProgressDialog();
					// 评论列表
					mList = (MyListView) findViewById(R.id.mListView);
					adapter = new DataAdapter(AddShareDiscussActivity.this);
					if (mDataList.size() != 0) {
						mRelativeLayout.setVisibility(View.VISIBLE);
						mList.setVisibility(View.VISIBLE);
					}

					mList.setAdapter(adapter);
				}

			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.warn(AddShareDiscussActivity.this, "与服务器连接失败，请检查网络");

			}
		}, DataStore.class, AppConfig.USER).execute(params);
	}

	/**
	 * 获取分享信息
	 */
	@SuppressWarnings("unchecked")
	private void getShareInfo() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryOneShareNew1"));
		// 添加要传递的参数

		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("shareId", shareId));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				stopProgressDialog();
				Gson gson = new Gson();
				String result = (String) object;
				try {
					mHomeInfoBean = gson.fromJson(result, HomeInfoBean.class);
				} catch (Exception e) {
					return;
				}

				if (mHomeInfoBean != null) {
					describe_info_relative.setVisibility(View.VISIBLE);
					userIcon = mHomeInfoBean.getIcon();
					time = mHomeInfoBean.getCreateTime();
					content = mHomeInfoBean.getContent();
					main_img = mHomeInfoBean.getThumbArr();
					main_img_big = mHomeInfoBean.getOriginalArr();
					name = mHomeInfoBean.getNickname();
					messageId = mHomeInfoBean.getMessageId();
					zanCount = mHomeInfoBean.getPraiseCount();
					is_zan = mHomeInfoBean.getPraise();
					ooflag = mHomeInfoBean.getOoFlag();
					address = mHomeInfoBean.getAddress();
					rbMsgId = mHomeInfoBean.getRbMsgId();
					ssType = mHomeInfoBean.getSsType();
					sectionId = mHomeInfoBean.getSectionId();
					if ("1".equals(mHomeInfoBean.getCanDel())) {
						delete.setVisibility(View.VISIBLE);
					} else {
						delete.setVisibility(View.GONE);
					}

					setInfo();
				} else {
					Alert.warn(AddShareDiscussActivity.this, "系统异常");

				}

			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.warn(AddShareDiscussActivity.this, "与服务器连接失败，请检查网络");

			}
		}, DataStore.class, AppConfig.USER).execute(params);
	}

	/**
	 * 删除分享
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void deleteSahre() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "delShareNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("shareId", shareId));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if ("0".equals(mCloudBeansBean.getResultCode())) {
					if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
						new PopupWindowAnimation(mainView, AddShareDiscussActivity.this);
					}
					stopProgressDialog();

					Alert.info(AddShareDiscussActivity.this, "删除成功");
					Intent i = new Intent();
					i.putExtra("delete", true);
					setResult(RESULT_OK, i);
					finish();
				} else {
					stopProgressDialog();
					Alert.error(AddShareDiscussActivity.this, result);
				}
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(AddShareDiscussActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

	/**
	 * 添加评论的回复
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void replyShareComment(String commentId, String replyUserId) {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "replyShareCommentNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("shareId", shareId));
		params.add(new BasicNameValuePair("commentUserId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("replyUserId", replyUserId));
		params.add(new BasicNameValuePair("content", edit_discuss.getText() + ""));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if ("0".equals(mCloudBeansBean.getResultCode())) {
					if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
						new PopupWindowAnimation(mainView, AddShareDiscussActivity.this);
					}
					stopProgressDialog();
					edit_discuss.getText().clear();
					imm.hideSoftInputFromWindow(edit_discuss.getWindowToken(), 0);
					Alert.info(AddShareDiscussActivity.this, "操作成功");
					getDiscussList();
				} else {
					stopProgressDialog();
					Alert.error(AddShareDiscussActivity.this, result);
				}
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(AddShareDiscussActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	public boolean dispatchKeyEvent(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getRepeatCount() == 0) {
			if (isreplyed && !isPraise) {
				Intent i = new Intent();
				i.putExtra("position", position);
				setResult(RESULT_OK, i);
				i.putExtra("isreplyed", true);
				isreplyed = false;
			} else if (isPraise && !isreplyed) {
				Intent i = new Intent();
				i.putExtra("position", position);
				i.putExtra("zanCount", zanCount);
				setResult(RESULT_OK, i);
				isPraise = false;
			} else if (isreplyed && isPraise) {
				Intent i = new Intent();
				i.putExtra("position", position);
				i.putExtra("zanCount", zanCount);
				i.putExtra("isreplyed", true);
				setResult(RESULT_OK, i);
				isPraise = false;
				isreplyed = false;
			} else {
				Intent i = new Intent();
				i.putExtra("position", position);
				setResult(RESULT_OK, i);

			}
			finish();
			return false;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}

	/**
	 * 我也要参加
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void joinToo(final String shareId, final String rbMsgId) {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "toRedBagNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("rbMsgId", rbMsgId));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}

				Intent intent = new Intent();
				intent.setClass(AddShareDiscussActivity.this, MainAlertActivity.class);
				intent.putExtra("couponTitle", mCloudBeansBean.getTitle());
				intent.putExtra("logo", mCloudBeansBean.getLogo());
				intent.putExtra("content", mCloudBeansBean.getContent());
				intent.putExtra("shareId", shareId);
				intent.putExtra("rbMsgId", rbMsgId);
				startActivity(intent);

			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(AddShareDiscussActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.GROUP).execute(params);
	}

	@Override
	public void onEmojiconClicked(Emojicon emojicon) {
		EmojiconsFragment.input(edit_discuss, emojicon);
	}

	@Override
	public void onEmojiconBackspaceClicked(View v) {
		EmojiconsFragment.backspace(edit_discuss);
	}
}
